//
/***********************************************************
             File name:      main.c
             Author:         Bitat (陆叁柒)
             Email :         Bitat@qq.com
             Gitee :         https://gitee.com/bitat
             CSDN  :         https://blog.csdn.net/bitat 
             History:        2023/2/12
************************************************************/

#include <stdio.h>
#include <math.h>
int main()
{
    int denominator, flag;
    double item, pi; //pi用于存放累加和
  
    /*循环初始化*/
    flag = 1;
    denominator = 1;
    item = 1.0;
    pi = 0;
  
    while (fabs(item) >= 0.0001) //当|item|>=0.0001时，执行循环
    {
        item = flag * 1.0 / denominator; //计算第i项的值
        pi = pi + item;                  //累加第i项的值
        flag = -flag;                    //改变符号
        denominator += 2;                //分母每次加2
    }
    pi = pi * 4; //循环结结果是pi/4
    printf("pi=%.4f\n", pi);
  
    return 0;
}  
